package com.wcy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.domain.WebConfig;
import com.wcy.model.R;
import com.wcy.service.WebConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * <p>Title:WebConfigController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/27 19:01</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@RestController
@Api(tags = "webConfig的控制器")
@RequestMapping("/webConfigs")
public class WebConfigController {

    @Autowired
    private WebConfigService webConfigService;

    @GetMapping
    @ApiOperation(value = "分页查询我们的webConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "webConfig的名称"),
            @ApiImplicitParam(name = "type",value = "类型"),
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小")
    })
    @PreAuthorize("hasAuthority('web_config_query')")
    public R<Page<WebConfig>> findByPage(String name,String type,@ApiIgnore Page<WebConfig> page){
        Page<WebConfig> webConfigPage=webConfigService.findByPage(page,name,type);
        return R.ok(webConfigPage);
    }

    @PostMapping
    @ApiOperation(value = "新增webConfig")
    @PreAuthorize("hasAuthority('web_config_create')")
    public R add(@RequestBody WebConfig webConfig){
        boolean save = webConfigService.save(webConfig);
        if(save){
            return R.ok();
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @ApiOperation(value = "修改webConfig")
    @PreAuthorize("hasAuthority('web_config_update')")
    public R update(@RequestBody WebConfig webConfig){
        boolean update = webConfigService.updateById(webConfig);
        if(update){
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除webConfig")
    @ApiImplicitParam(name = "ids",value = "要删除的id数组")
    @PreAuthorize("hasAuthority('web_config_delete')")
    public R delete(@RequestBody Long[] ids){
        boolean removeByIds = webConfigService.removeByIds(Arrays.asList(ids));
        if(removeByIds){
            return R.ok();
        }
        return R.fail("删除失败");
    }
}
